package com.observer.gsj20150607;

import java.util.ArrayList;

public class WeatherData implements Subject{
	
	private ArrayList<Observer> observers;
	private float temperature;
	private float pressure;
	
	public WeatherData(){
		observers = new ArrayList<Observer>();
	}

	@Override
	public void registerObservers(Observer o) {
		observers.add(o);
	}

	@Override
	public void removeObservers(Observer o) {
		int i = observers.indexOf(o);
		if(i>=0){
			observers.remove(o);
		}
	}

	@Override
	public void notifyObservers() {
		for(int i = 0;i < observers.size();i++){
			Observer observer = (Observer)observers.get(i);
			observer.update(temperature, pressure);
		}
	}
	
	public void dataChanged(){
		notifyObservers();
	}
	
	//假装抓取数据
	public void setData(float temperature, float pressure){
		this.temperature = temperature;
		this.pressure = pressure;
		dataChanged();
	}

}
